package com.gavindon.android.widget

import android.content.Context
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.LayerDrawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.StateListDrawable
import android.graphics.drawable.shapes.RectShape
import android.graphics.drawable.shapes.RoundRectShape
import android.util.AttributeSet
import android.widget.EdgeEffect
import android.widget.FrameLayout
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.leanback.widget.FocusHighlightHelper
import com.gavindon.android.R
import org.jetbrains.anko.dip
import android.graphics.RectF
import kotlin.math.round


/**
 * description:
 * Created by liNan on  2022-2-5 20:55
 */
class GradientDrawableFrameLayout : FrameLayout {


    private val stateFocus = android.R.attr.state_focused
    private var gradientColor: IntArray
    private var roundCorner = dip(10).toFloat()

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val ta = context.theme.obtainStyledAttributes(
            attrs,
            R.styleable.GradientDrawableFrameLayout,
            defStyleAttr,
            0
        )
        val startColor =
            ta.getColor(R.styleable.GradientDrawableFrameLayout_color_start, Color.GREEN)
        val endColor = ta.getColor(R.styleable.GradientDrawableFrameLayout_color_end, Color.GREEN)
        roundCorner = ta.getDimension(R.styleable.GradientDrawableFrameLayout_round, 10f)

        ta.recycle()

//        descendantFocusability = FOCUS_AFTER_DESCENDANTS
        gradientColor = intArrayOf(startColor, endColor)
        init()
    }

    private fun init() {
        background = stateListDrawable()
        FocusViewUtils.scaleView(this, 0)
    }

    private fun stateListDrawable(): StateListDrawable {
        val stateListDrawable = StateListDrawable()
        stateListDrawable.addState(
            intArrayOf(stateFocus),
            effect()
        )
        stateListDrawable.addState(
            intArrayOf(),
            getGradientDrawable()
        )
        return stateListDrawable
    }

    private fun getGradientDrawable(): GradientDrawable {
        val gradientDrawable =
            GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, gradientColor)
        gradientDrawable.cornerRadius = roundCorner
        return gradientDrawable
    }

    private fun effect(): LayerDrawable {
        // 外部矩形弧度
        val corner = roundCorner
        val outerR = floatArrayOf(corner, corner, corner, corner, corner, corner, corner, corner)
        // 内部矩形与外部矩形的距离
        val inset = RectF(0f, 0f, 0f, 0f)
        // 内部矩形弧度
        val innerRadii =
            floatArrayOf(corner, corner, corner, corner, corner, corner, corner, corner)
        val shapeDrawable = ShapeDrawable(RoundRectShape(outerR, null, null))
        shapeDrawable.paint.color = Color.WHITE
        val layers = arrayOf(shapeDrawable, getGradientDrawable())
        val layerDrawable = LayerDrawable(layers)
        layerDrawable.setLayerInset(0, -1, -1, -1, -2)

        return layerDrawable
    }

}